$(document).ready(function () {
    var app, operation, size;
    $('button[data-target="#select"]').click(function () {
        $('#application').change(function () {
            var operation = $('#operation');
            switch ($('#application').val()) {
                case '0':
                {
                    operation.empty();
                    operation.append($('<option></option>').html('All').attr('value', 0)).append($('<option></option>').html('Add').attr('value', 1)).append($('<option></option>').html('Delete').attr('value', 2));
                }
                    break;
                case '1':
                {
                    operation.empty();
                    operation.append($('<option></option>').html('All').attr('value', 0)).append($('<option></option>').html('Login').attr('value', 1)).append($('<option></option>').html('Logout').attr('value', 2));
                }
                    break;
            }
        });
    });
    $('#selectButton').click(function () {
        app = $('#application').val();
        operation = $('#operation').val();
        size = $('#size').val();
        $('#selectForm').submit(function () {
            return false;
        });
        switch (app) {
            case '0':
            {
                $.ajax({
                    url: '/getRBACLogs',
                    method: 'post',
                    data: {
                        operation: operation,
                        size: size
                    },
                    dataType: 'json'
                }).done(function (data) {
                    updateRBACLogs(data);
                    $('#select').modal('hide');
                })
            }
                break;
            case '1':
            {
                $.ajax({
                    url: '/getAppLogs',
                    method: 'post',
                    data: {
                        'operation': operation,
                        'size': size
                    },
                    dataType: 'json'
                }).done(function (data) {
                    console.log(data);
                    updateAppLogs(data);
                    $('#select').modal('hide');
                })
            }
                break;
        }
    });
    $('#deleteButton').click(function () {
        $('#deleteForm').submit(function () {
            return false;
        });
        $.ajax({
            url: '/deleteLogs',
            method: 'post',
            data: {
                deleteApp: $('#deleteApp').val(),
                time: $('#time').val()
            }
        }).done(function () {
            $('#delete').modal('hide');
            location.reload();
        });
    });
});

function updateRBACLogs(data) {
    if (data.result === true) {
        $('legend').html($('legend').html() + '(Full)');
    }
    var thead = $('thead');
    var tbody = $('tbody');
    thead.empty();
    thead.append($('<tr><th>Table Name</th><th>Operation</th><th>Content</th><th>Time</th></tr>'));
    tbody.empty();
    for (i in data.logs) {
        var tr = $('<tr></tr>');
        tr.append($('<td></td>').html(data.logs[i].tablename)).append($('<td></td>').html(data.logs[i].operation === 1 ? 'Add' : 'Delete')).append($('<td></td>').html(data.logs[i].content)).append($('<td></td>').html(data.logs[i].time));
        tbody.append(tr);
    }
}

function updateAppLogs(data) {
    if (data.result === true) {
        $('legend').html($('legend').html() + '(Full)');
    }
    var thead = $('thead');
    var tbody = $('tbody');
    thead.empty();
    thead.append($('<tr><th>Username</th><th>Operation</th><th>Time</th></tr>'));
    tbody.empty();
    for (i in data.logs) {
        var tr = $('<tr></tr>');
        tr.append($('<td></td>').html(data.logs[i].username)).append($('<td></td>').html(data.logs[i].operation === 1 ? 'Login' : 'Logout')).append($('<td></td>').html(data.logs[i].time));
        tbody.append(tr);
    }
}